package com.sunny.ynhz.servicebestpractice;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.sunny.ynhz.R;

public class Test extends AppCompatActivity implements View.OnClickListener {

    //    private DownloadService.DownloadBinder downloadBinder;
    private DownloadTask downloadTask;
    private String url = "http://oss.taiyangos.com/down/gpsbd/GPSBD-v1.4.1.apk";
    private Button startDownload;
    //
//    private ServiceConnection connection = new ServiceConnection() {
//
//        @Override
//        public void onServiceConnected(ComponentName name, IBinder service) {
//            downloadBinder = (DownloadService.DownloadBinder) service;
//        }
//
//        @Override
//        public void onServiceDisconnected(ComponentName name) {
//
//        }
//    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        startDownload = (Button) findViewById(R.id.start_download);
        startDownload.setOnClickListener(this);
        Button pauseDownload = (Button) findViewById(R.id.pause_download);
        pauseDownload.setOnClickListener(this);
        Button cancelDownload = (Button) findViewById(R.id.cancel_download);
        cancelDownload.setOnClickListener(this);
//        Intent intent = new Intent(this, DownloadService.class);
//        //这一点至关重要，因为启动服务可以保证DownloadService一直在后台运行，绑定服务则可以让MaiinActivity和DownloadService
//        //进行通信，因此两个方法的调用都必不可少。
//        startService(intent);  //启动服务
//        bindService(intent, connection, BIND_AUTO_CREATE);//绑定服务
    }

    public void onClick(View v) {
//        if (downloadBinder == null) {
//            return;
//        }
        switch (v.getId()) {
            case R.id.start_download:

                downloadTask = new DownloadTask(new DownloadListener() {
                    @Override
                    public void onProgress(int progress) {
                        startDownload.setText(progress+"");


                    }

                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onFailed() {

                    }

                    @Override
                    public void onPaused() {

                    }

                    @Override
                    public void onCanceled() {

                    }
                });
                downloadTask.execute(url);

                break;
            case R.id.pause_download:
//                downloadBinder.pauseDownload();
                downloadTask.pauseDownload();
                break;
            case R.id.cancel_download:
//                downloadBinder.cancelDownload();
                downloadTask.cancelDownload(url);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除绑定服务
//        unbindService(connection);
    }
}
